package com.sq.partner.manager.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.sq.partner.manager.dao.INlifeConDao;
import com.sq.partner.manager.mybatis.model.NlifeCon;
import com.sq.partner.manager.util.DBUtil;
import com.sq.partner.manager.util.LogUtil;
import com.sq.partner.manager.util.Page;

@Repository
public class NlifeConDaoImpl extends BaseDaoImpl implements INlifeConDao {
	private static Logger logger = LogUtil.getLog(NlifeConDaoImpl.class);

	@Override
	public Page<NlifeCon> queryProdConInsureInfoPage(Page<NlifeCon> page, String prodCode) throws Exception {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try{
			conn = this.getConnection();
			String sql = "SELECT T.PROD_CODE AS PRODCODE,"
       +"T.COMPANY_CODE AS COMPANYCODE,"
       +"T.COMPANY_NAME AS COMPANYNAME,"
       +"T.CON_RATE * 100 AS CONRATE,"
       +"T.CHIEF_FLAG AS CHIEFFLAG,"
       +"DECODE(T.CHIEF_FLAG, '0', '否', '1', '是', '') AS CHIEFFLAGDSCP "
       +"FROM T_NLIFE_CON T "
       +"WHERE T.PROD_CODE = '"+prodCode+"' "
       +"ORDER BY T.COMPANY_CODE ASC";
			logger.debug("执行SQL：" + sql);
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			List lists = DBUtil.transRs2List(rs);
			page.setData(lists);
		}finally{
			close(rs);
			close(pstmt);
			close(conn);
		}
		return page;
	}

	@Override
	public boolean saveProdConInsure(NlifeCon nc) throws Exception {
		return this.add("insertNlifeCon", nc);
	}

	@Override
	public boolean deleteProdConInsureByProdCode(String prodCode)
			throws Exception {
		return this.delete("delNlifeConById", prodCode);
	}

	@Override
	public boolean searchProdConInsureInfoByProdCode(String prodCode)
			throws Exception {
		int a = (Integer) this.query("isExistNewLifeProdConInsuCountByProdCode", prodCode);
		if(a>0){
			return true;
		}
		return false;
	}
 
}
